/*
 * register.c
 *
 *  Created on: Feb 4, 2012
 *      Author: Craig Markham
 *		Class:  TCSS 371
 *		Assignment:
 */

#include "register.h"

/*
 * =======================================================================================
 * Register implementation
 * =======================================================================================
 */

/**
 * Register init
 * creates register object and puts a random value from 0 to 65535 into it
*/
void register_init(RegisterPtr reg_ptr){
  srand(time(NULL));
  * reg_ptr = rand() % 0xFFFF;
};

/**
 * Register putRegValue
 * puts ushort into register, returns error
*/
int register_putRegValue(RegisterPtr reg_ptr, ushort val){
  * reg_ptr = val;
  return (int) val;
};

/**
 * Register putLowByte
 * puts a uchar into the LOB of the register, returns error
*/
int register_putLowByte(RegisterPtr reg_ptr, uchar lob){
  int error = 0;
  uchar hob = * reg_ptr >> 8;// clear lob
  * reg_ptr = ((ushort) hob << 8) | lob;
  return error;
};

/**
 * Register putHighByte
 * puts a uchar into the HOB of the register, returns error
*/
int register_putHighByte(RegisterPtr reg_ptr, uchar hob){
  int error = 0;
  * reg_ptr = ((ushort) hob) << 8 | (uchar) * reg_ptr;
  return error;
};

/**
 * Register getRegValue
 * the value in the register, sets error
*/
int register_getRegValue(RegisterPtr reg_ptr, int error){
  return * reg_ptr;
};

/**
 * Register getLowByte
 * the LOB of the register, sets error
*/
uchar register_getLowByte(RegisterPtr reg_ptr, int * error){
  return * reg_ptr;
};

/**
 * Register getHighByte
 * the HOB of the register, sets error
*/
uchar register_getHighByte(RegisterPtr reg_ptr, int * error){
  return * reg_ptr >> 8;
};

/**
 * Register signOf
 * returns 0 or 1, 2 or greater error code
*/
int register_signOf(RegisterPtr reg_ptr){
  return * reg_ptr>>15;
};
